// ==UserScript==
// @name           bro3_domestic
// @namespace      ronf-bro3
// @include        http://*.3gokushi.jp/card/domestic_setting.php*
// @require        http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js
// ==/UserScript==




if (location.pathname == "/card/domestic_setting.php") {
	var u_count = $('input[value="設定されている武将を解除する"]').length;
	if (u_count == 0) {
		$('table.general').each(function(i, e) {
			$('th:last-child', e).after($('<th class="bundle_select">一括選択</th>'));
			$('td:last-child', e).after($('<td><input type="checkbox" name="bundle_select" value="' + $('td input[name="id"]', e).val() + '"/></td>'));
		});
		var actionButton = $('<input type="button" name="bundle_action" value="一括設定" />');
		actionButton.click(function(e) {
			var targets = $('input:checked[name="bundle_select"]');
			if (targets.length > 0) {
				targets.map(function(i, e) {
					var url = "http://" + location.hostname + "/card/domestic_setting.php?id=" + $(e).val() + "&mode=domestic";
					return function(){$.get(url);};
				}).each(function(i, e) {
					setTimeout(e, 500);
				});
				setTimeout(function() {location.reload();}, 1000);
			}
		});
		$('div.centering-submit').append(actionButton);
	} else if (u_count > 1) {
		var actionButton = $('<input type="button" name="bundle_action" value="一括で設定されている武将を解除する" />');
		actionButton.click(function(e) {
			var targets = $('input[name="id"]');
			targets.each(function(i, e) {
				setTimeout(function() {
					var url = "http://" + location.hostname + "/card/domestic_setting.php?id=" + $(e).val() + "&mode=u_domestic";
					$.get(url);
				}, 500);
			});
			setTimeout(function() {location.reload();}, 1000);
		});
		$('form[action="/card/domestic_setting.php"]:last-child').after(actionButton);
	}
}
